import redis
import time
import csv
import pymysql
import config
conn = pymysql.connect(**config.mysql_option)
cur = conn.cursor()
r = redis.StrictRedis(**config.redis)



'''
 redis 数据结构 
    [
     主键  ""  plat_id third_id account_id account_name plat_name user_id title_name flow_count add_time url tag adv_id audit_status
                %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
   
    ]
'''


def store_info_miaopai(item,user_info):
    tmp = ['','14',str(item["scid"]),str(user_info['account_id']),user_info['account_name'],user_info['plat_name'],str(user_info['user_id']),
                item['title'],str(item['vcnt']),item['createtime'],item['video_url'],'0','0','1'

           ]
    tmp = "|||".join(tmp).encode()
    r.append("med_flow_miaopai", tmp)
    r.append("med_flow_miaopai", ";")


def store_info_bilibili(item):
    tmp = ['', '2', str(item['third_id']), str(item['account_id']), item['account_name'],
           item['plat_name'], str(item['user_id']), item['title_name'], str(item['flow_count']),
           item['add_time'], item['url'], '0', '0', '1'

           ]
    tmp = "|||".join(tmp).encode('UTF-8')
    r.append("med_flow_bilibili", tmp)
    r.append("med_flow_bilibili", ";")


def store_info_duanzi(item):
    tmp = ['', '3', str(item['third_id']), str(item['account_id']), item['account_name'],
           item['plat_name'], str(item['user_id']), item['title_name'], str(item['flow_count']),
           item['add_time'], item['url'], '0', '0', '1'

           ]
    tmp = "|||".join(tmp).encode('UTF-8')
    r.append("med_flow_duanzi", tmp)
    r.append("med_flow_duanzi", ";")


def store_info_renren(item,user_info):
    '''
    {u'updateTime': 1503300319000, u'silverCount': 0,
    u'playLink': u'http://sh.file.myqcloud.com/files/v2/1252816746/rrsp/2017/08/21/c09f83759276479f89af4b24be4675d2.mp4', u'sequence': None, u'failMsg': None, u'uploadType': u'upload',
     u'videoDurationStr': u'00:02:00', u'parentCategoryList': None, u'videoStatusType': u'pass', u'aliasName': None,
     u'id': 272254, u'category': u'\u79d1\u6280', u'parentCategory': None,
     u'title': u'\u6709\u8fd9\u5c0f\u76d2\u5b50\uff0c\u5c31\u80fd\u5149\u660e\u6b63\u5927\u5077\u7535\uff0c\u6709\u5b83\u6c38\u8fdc\u4e0d\u7528\u4ea4\u7535\u8d39', u'parentCategoryId': None,
     u'brief': u'\u6709\u8fd9\u5c0f\u76d2\u5b50\uff0c\u5c31\u80fd\u5149\u660e\u6b63\u5927\u5077\u7535\u7f51\u7684\u7535\uff0c\u5bb6\u91cc\u6709\u5b83\u6c38\u8fdc\u4e0d\u7528\u4ea4\u7535\u8d39\uff01', u'createTimeStr': u'1\u5c0f\u65f6\u524d', u'authorName': u'\u627e\u9753\u673a', u'subjectName': None, u'categoryIdList': None, u'recommend': False, u'commentCount': 2, u'categoryList': None, u'categoryId': 8, u'updateTimeStr': u'47\u5206\u949f\u524d', u'danmuCount': 0, u'tags': u'[{"name":"\u7535\u6ce2","id":78977},{"name":"\u7535\u8d39","id":58548},{"name":"\u624b\u673a","id":4569},{"name":"\u5145\u7535\u5b9d","id":12703},{"name":"\u521b\u610f","id":457},{"name":"\u53d1\u660e","id":1003},{"name":"\u521b\u9020","id":27078}]', u'verticalCover': u'https://img.rr.tv/video/20170821/o_1503298020938.jpg', u'parentCategoryIdList': None, u'comeFrom': u'diy', u'authorId': 11437246, u'watchLevel': 0, u'playCount': 274, u'hasSubject': False, u'createTime': 1503298067000, u'tagList': [{u'times': 0, u'id': 78977, u'name': u'\u7535\u6ce2'}, {u'times': 0, u'id': 58548, u'name': u'\u7535\u8d39'}, {u'times': 0, u'id': 4569, u'name': u'\u624b\u673a'}, {u'times': 0, u'id': 12703, u'name': u'\u5145\u7535\u5b9d'}, {u'times': 0, u'id': 457, u'name': u'\u521b\u610f'}, {u'times': 0, u'id': 1003, u'name': u'\u53d1\u660e'}, {u'times': 0, u'id': 27078, u'name': u'\u521b\u9020'}], u'videoDuration': 120, u'cover': u'https://img.rr.tv/video/20170821/o_1503298004215.jpg', u'heat': 0, u'original': None}
    [
     主键  ""  plat_id third_id account_id account_name plat_name user_id title_name flow_count add_time url tag adv_id audit_status

    '''
    add_time = int(item['updateTime'])
    x=time.localtime(int(add_time / 1000))
    add_time=time.strftime("%Y-%m-%d %X",x)
    tmp = ['', '8', str(item['id']), str(user_info['account_id']), user_info['account_name'],
           user_info['plat_name'], str(user_info['user_id']), item['title'], str(item['playCount']),
           add_time, item['playLink'], '0', '0', '1'

           ]
    tmp = "|||".join(tmp).encode('UTF-8')
    r.append("med_flow_renren", tmp)
    r.append("med_flow_renren", ";")


def store_info_qq(item,user_info):
    # plat_id = 7
    tmp = [
       "", "7",str(item['article_id']),str(user_info['account_id']),user_info['account_name'],user_info['plat_name'],str(user_info['user_id']),item['title'],str(item['article_total_statistics']['play']),item['pub_time'],
        item['url'],"0","0","1"
    ]
    tmp = "|||".join(tmp).encode()

    r.append("med_flow_qq",tmp)
    r.append("med_flow_qq",";")


def store_info_toutiao(item):

    user_info = item['user_info']
    timeamp = item['behot_time']
    x = time.localtime(timeamp)
    time_str = time.strftime('%Y-%m-%d %H:%M:%S',x)
    urls = "https://www.toutiao.com" + item['source_url']
    tmp = ["",str(item['plat_id']) , str(item['item_id']),str(user_info['account_id']),
           user_info['account_name'].replace("\n",""),user_info['plat_name'],str(user_info['user_id']),
           item['title'],str(item['detail_play_effective_count']),time_str,urls,'0','0','1'
           ]
    try:
        tmp1 = ("|||".join(tmp)).encode('GBK')
    except Exception as e:
        tmp1 = "|||".join(tmp)
    r.append("med_flow_toutiao", tmp1)
    r.append("med_flow_toutiao", ";")


def store_info_yidian(item,info):
    '''
    主键  ""  plat_id third_id account_id account_name plat_name user_id title_name flow_count
     add_time url tag adv_id audit_status

    plat_id 9
    ,9,32553291,22,一点号,2,这种材料从废手机里回收，1克能卖8亿人民币，抠下来你就发了！,0,2017-08-22 13:43:47,http://video.yidianzixun.com/video/get-url?key=user_upload/15033805118792078fb6b4b4f3d9de44a4d003abc6673.mp4,0,0,1

    '''


    timeamp = int(item['created_time'] / 1000)
    now = time.strftime('%Y-%m-%d %X',time.localtime(timeamp))
    if item['feature']['original'].get("title"):
        tmp = ["",'9',str(item['id']),str(info['account_id']),info['account_name'],info['plat_name'],
               str(info['user_id']),item['feature']['original']['title'],str(item['data']['viewDoc']),now,
               item['feature']['video_url'],
               '0','0','1'
               ]
        tmp = '|||'.join(tmp)

        r.append("med_flow_yidian",tmp)
        r.append("med_flow_yidian",';')

    else:
        pass
